package com.github.pig.nideshop.controller.user;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.ZyResult;
import com.github.pig.nideshop.entity.NideshopTrade;
import com.github.pig.nideshop.service.INideshopTradeDetailService;
import com.github.pig.nideshop.service.INideshopTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author J神
 * @since 2018-06-23
 */
@RestController
@RequestMapping("/trades")
public class NideshopTradeController {

    @Autowired
    private INideshopTradeService nideshopTradeService;
    @Autowired
    private INideshopTradeDetailService nideshopTradeDetailService;

    /**
     * 交易记录
     * @param nideshopTrade
     * @param page
     * @return
     */
    @GetMapping
    public ZyResult trades(NideshopTrade nideshopTrade,Page page){
        Page<NideshopTrade> trades=nideshopTradeService.selectPage(page,new EntityWrapper<>(nideshopTrade));
        List<NideshopTrade> records=trades.getRecords();
        List<Integer> ids=new ArrayList<>();
        for(NideshopTrade t:records){
                ids.add(t.getId());
        }
        EntityWrapper ex=new EntityWrapper();
        ex.in("trade_id",ids);
        List tradesDetails=nideshopTradeDetailService.selectList(ex);
        Map map=new HashMap<>();
        map.put("trades",trades);
        map.put("tradesDetails",tradesDetails);
        return ZyResult.ok(map);
    }
    /**
     * 发起充值
     */
    @PostMapping("/charge")
    public ZyResult trade(NideshopTrade nideshopTrade){
        nideshopTradeService.trade(nideshopTrade,null);
       return ZyResult.ok();
    }

}

